<?php

class SlideController extends AdminController {

    public $parentUrl;

    public function init() {
        parent::init();
        $this->parentUrl = $this->module->id . '/' . Yii::app()->getController()->getId();
    }

    public function actionIndex() {
        $model = Slide::model()->findAll();
        $this->render('index', array(
            'items' => $model,
            'parentUrl' => $this->parentUrl
        ));
    }

    public function actionCreate() {
        $model = new Slide();
        if (isset($_POST['Slide'])) {
            if (isset($_POST['Slide'])) {
                $image = CUploadedFile::getInstancesByName('items');
                if (isset($image)) {
                    $i = 0;
                    foreach ($image as $key => $value) {
                        $i++;
                        $model = new Slide();
                        $model['file_ext'] = $value->type;
                        $model['old_name'] = $value->name;
                        $ran = rand(0, 999999999);
                        $fileName = trim(date('Y-m-d H-i-s') . $ran) . '.' . $value->extensionName;
                        $model['name'] = $fileName;
                        $model['url'] = Yii::app()->basePath . "/../data/image/slide/";
                        $model['active'] = 1;
                        $model['order'] = $i;
                        $model->save();
                        $value->saveAs(Yii::app()->basePath . "/../data/image/slide/$fileName");
                    }
                }
                $this->redirect(Yii::app()->createAbsoluteUrl('systems/slide/index'));
            }
        }
        $this->render('create', array(
            'model' => $model
        ));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id);
        if (isset($_POST['Slide'])) {
            $model->attributes = $_POST['Slide'];
            $model['order'] = $_POST['Slide']['order'];
            $model['active'] = $_POST['Slide']['active'];
            $model->validate();
            if (!$model->hasErrors()) {
                if ($model->save()) {
                    $this->redirect(Yii::app()->createAbsoluteUrl('systems/slide/index'));
                }
            }
        }
        $this->render('update', array(
            'model' => $model,
            'parentUrl' => $this->parentUrl
        ));
    }

    public function actionDelete($id) {
        try {
            if ($model = $this->loadModel($id)) {
                if(file_exists(Yii::app()->basePath . "/../data/image/slide/$model->name")){
                    unlink(Yii::app()->basePath . "/../data/image/slide/$model->name");
                }
                if ($model->delete()) {
                    $this->redirect(Yii::app()->createAbsoluteUrl('systems/slide/index'));
                }
            }
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function loadModel($id) {
        try {
            $model = Slide::model()->findByPk($id);
            if ($model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
            return $model;
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

}

?>
